/**
 * 纯函数提到这里来写
 */

/**
 * 分页数据, 和分页组件搭配使用
 */
export function initPageInfo(size: number = 10) {
  return {
    /** 条数 */
    pageSize: size,
    /** 页码 */
    currentPage: 1,
    /** 总数 */
    total: 0,
  };
}

/**
 * 检测变量类型
 * @param target 检测的目标
 */
export function getType(target: any) {
  const value = Object.prototype.toString.call(target);
  const result = (value.match(/\[object (\S*)\]/) as RegExpMatchArray)[1];
  return result.toLocaleLowerCase() as JavaScriptTypes;
}

/**
 * 修改属性值-逐个属性深层修改|只修改之前存在的值
 * @param target 修改的目标
 * @param value 修改的内容
 */
export function modifyData<T>(target: T, value: T) {
  for (const key in value) {
    if (Object.prototype.hasOwnProperty.call(target, key)) {
      if (Object.prototype.toString.call(target[key]) === "[object Object]") {
        modifyData(target[key], value[key]);
      } else {
        target[key] = value[key];
      }
    }
  }
}

/**
 * 设置属性值-之前不存在的值直接加上
 * @param target 设置的目标
 * @param value 设置的内容
 */
export function setData<T>(target: T, value: T) {
  for (const key in value) {
    if (Object.prototype.hasOwnProperty.call(target, key)) {
      if (Object.prototype.toString.call(target[key]) === "[object Object]") {
        modifyData(target[key], value[key]);
      } else {
        target[key] = value[key];
      }
    } else {
      target[key] = value[key];
    }
  }
}

/**
 * 格式化日期
 * @param value 指定日期
 * @param format 格式化的规则
 * @example
 * ```js
 * formatDate();
 * formatDate(1603264465956);
 * formatDate(1603264465956, 'h:m:s');
 * formatDate(1603264465956, 'Y年M月D日');
 * ```
 */
export function formatDate(value: string | number | Date = Date.now(), format = "Y-M-D h:m:s") {
  if (["null", null, "undefined", undefined, ""].includes(value as any)) return "";
  // ios 和 mac 系统中，带横杆的字符串日期是格式不了的，这里做一下判断处理
  if (typeof value === "string" && new Date(value).toString() === "Invalid Date") {
    value = value.replace(/-/g, "/");
  }
  const formatNumber = (n: number) => `0${n}`.slice(-2);
  const date = new Date(value);
  const formatList = ["Y", "M", "D", "h", "m", "s"];
  const resultList = [];
  resultList.push(date.getFullYear().toString());
  resultList.push(formatNumber(date.getMonth() + 1));
  resultList.push(formatNumber(date.getDate()));
  resultList.push(formatNumber(date.getHours()));
  resultList.push(formatNumber(date.getMinutes()));
  resultList.push(formatNumber(date.getSeconds()));
  for (let i = 0; i < resultList.length; i++) {
    format = format.replace(formatList[i], resultList[i]);
  }
  return format;
}

/**
 * 深拷贝
 * @param obj
 */
export function deepCopy<T extends { [key: string]: any }>(obj: T): T {
  if (obj === null) return obj;
  if (typeof obj !== "object") return obj;
  let result: { [key: string]: any } = Array.isArray(obj) ? [] : {};
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === "object" && obj[key] !== null) {
        result[key] = deepCopy(obj[key]); //递归拷贝
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result as T;
}
